package lp.controller;

import lp.service.lpService;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import test.JedisUtil;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import java.util.List;

/**
 * Created by Administrator on 2016/8/29.
 */

@Controller
@RequestMapping(value = "/Test/*") //访问的url地址前缀，可以不写，写了就必须在方法url前面先加上class url 进行区分控制器
public class TestController
{
    @Autowired
    private lpService service;
    //访问地址：http://localhost:8080/Test/returnSuccess
    @RequestMapping(value = "returnSuccess")    //实际访问的url地址
    public String returnSuccess() {

        service.sendMessage();
        return "/Views/success";    //返回Views文件夹下的success.jsp页面
    }

    //访问地址：http://localhost:8080/Test/returnString
    @RequestMapping(value = "returnString", produces = {"text/plain;charset=UTF-8"})
    //produces用于解决返回中文乱码问题，application/json;为json解决中文乱码
    @ResponseBody       //用于返回字符串,不写即返回视图
    public String returnString() {
        return "hello return string 这是中文，并没有乱码";
    }

    //访问地址：http://localhost:8080/Test/scan_number
    @RequestMapping(value = "scan_number")
    public String returnScanNumber(ModelMap model) {
        Jedis jedis =new  JedisUtil().getJedis();
        jedis.incr("count");
        model.put("count",jedis.get("count"));
        jedis.close();
        return "/Views/scanNumber";
    }
    //访问地址：http://localhost:8080/Test/login
    @RequestMapping(value = "login")
    public String login(@RequestParam("name")String name) {
        Jedis jedis =new  JedisUtil().getJedis();
        jedis.lpush("users",name);
        List<String> list = jedis.lrange("users",0,jedis.llen("users"));
        for(long i=jedis.llen("users")-1;i>=0;i--){
            System.out.println(list.get((int)i));
        }
        jedis.close();
        return "/Views/login";
    }

}